Telegram Group & Telegram Channel
🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections



tg-me.com/pro_python_code/1841
Create:
Last Update:

🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections

BY Python RU


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pro_python_code/1841

View MORE
Open in Telegram


Python RU Telegram | DID YOU KNOW?

Date: |

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

What is Telegram Possible Future Strategies?

Cryptoassets enthusiasts use this application for their trade activities, and they may make donations for this cause.If somehow Telegram do run out of money to sustain themselves they will probably introduce some features that will not hinder the rudimentary principle of Telegram but provide users with enhanced and enriched experience. This could be similar to features where characters can be customized in a game which directly do not affect the in-game strategies but add to the experience.

Python RU from jp


Telegram Python RU
FROM USA